//----------------------------------------------------------------------------
// pick.cpp
//
// display dialog allowing user to pick from list
//
// Copyright (C) 2011 Neil Butterworth
//----------------------------------------------------------------------------

#include <windows.h>
#include <string>
#include <iostream>
#include "command.h"
#include "cmdline.h"
#include "util.h"
#include "resource.h"
#include "error.h"

using namespace std;

namespace Swine {

//----------------------------------------------------------------------------
// Command & option names
//----------------------------------------------------------------------------

const char * const PICK_CMD 		= "pick";
const char * const CAPTION_OPT 	= "caption";

//----------------------------------------------------------------------------
// Help
//----------------------------------------------------------------------------

const char * const PICK_HELP =
	"select text from list box - texts must be suplied via standard input \n"
	"options:\n"
	" --caption     specify message box caption (default is 'Pick One') \n";


//----------------------------------------------------------------------------
// Command class and registration
//----------------------------------------------------------------------------

class PickCmd : public Command {

	public:

		PickCmd() : Command( PICK_CMD, PICK_HELP ) {}

		BOOL ProcessControlMessages( HWND wnd, int ctrl, int msg ) const;
		int Execute( CmdLine & cl ) const;
		void InitDialog( HWND dlg, CmdLine & cl ) const;
};

static AddCommand <PickCmd> cmd1_;

//----------------------------------------------------------------------------
// Not a lot to do here.
//----------------------------------------------------------------------------

int PickCmd :: Execute( CmdLine & cl ) const {
	DoDialog( IDD_PICKDLG, cl );
	return 0;
}

//----------------------------------------------------------------------------
// If the user clicked CANCEL, return empty string. If the clicked OK or
// double-cliked on the list, return list selection text.
//----------------------------------------------------------------------------

BOOL PickCmd :: ProcessControlMessages( HWND hwnd, int ctrl, int msg ) const {
	if ( ctrl == IDCANCEL && msg == BN_CLICKED ) {
		Reply( "" );
		DestroyWindow( hwnd );
		return TRUE;
	}
	else if ( (ctrl == IDOK && msg == BN_CLICKED) ||
				(ctrl == IDC_PICKLIST && msg == LBN_DBLCLK) ) {
		string picked = GetListBoxSelection( hwnd, IDC_PICKLIST  );
		Reply( picked );
		DestroyWindow( hwnd );
		return TRUE;
	}
	else {
		return TRUE;
	}
}

//----------------------------------------------------------------------------
// Set caption from command line. Read standard input and populate list.
//----------------------------------------------------------------------------

void PickCmd :: InitDialog( HWND dlg, CmdLine & cl ) const {

	string caption = cl.ExtractOption( CAPTION_OPT, "Pick One" );
	cl.MustBeEmpty();

	SetWindowText( dlg, caption.c_str() );
	string line;
	HWND lb = MustGetControl( dlg, IDC_PICKLIST );
	int n = 0;
	while( std::getline( cin, line ) ) {
		n++;
		SendMessage( lb, LB_ADDSTRING, 0, (LPARAM) line.c_str() );
	}
	if ( n == 0 ) {
		throw Error( "Nothing to pick from!" );
	}
	::SetFocus( lb );
	SendMessage( lb, LB_SETCURSEL, 0, 0 );

}


} // namespace
